梦入琼楼寒有月,行过石树冻无烟

Laravel Email 认证

Laravel 为开发者提供了一种非常简便的方法来实现出 Email 认证,很遗憾的是官方文档所提供的信息并不完整。

发送

MustVerifyEmail

首先,我们直接在 app/User.php 加上 MustVerifyEmail 接口使让其成为 User.php 的接口即可:

1
2
3
4
5
6
7
8
9
10
11
12
<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
……
}

.env

当配置接口类之后,我们还需要配置 .env 文件,并在此修改其邮箱的服务器以用于发送邮件:

1
2
3
4
5
6
7
8
MAIL_DRIVER=null
MAIL_HOST=null
MAIL_PORT=null
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

保护路由

Laravel 的 Auth\VerificationConteroller 类包含了发送验证链接和看验证 Email 的必要逻辑。这都将通过 verify 选项传递给 Auth::routes方法中,因此我们需要在 web.php 中增加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Auth::routes(['verify'=>true]);
Route::group([],function () {
Route::get('/home', 'HomeController@space')->name('home')->middleware('verified');
});

当我们在 get 路由后加入 middleware('verified') 方法后,将会自动验证当前用户使用邮箱是否经过验证,通过验证的用户将会在 users 数据库表中 email_verified_at字段中添加其验证时间。

验证重定向

如果要设置验证完成后主要通过 app/Http/Controllers/Auth/VerificationController.php 类中的 $redirectTo 属性来进行设置,默认的是跳转至用户的个人空间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\VerifiesEmails;

class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/

use VerifiesEmails;

/**
* Where to redirect users after verification.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;

……
}

如果在登录后验证邮箱时出错,可通过下述两条 artisan 命令来清理下 Laravel 缓存:

1
2
php artisan config:cache
php artisan cache:clear

文本

邮箱验证

Notifications

在 Laravel 中,我们可以通过使用 php artisan make:notification VerifyEmail 来新建一个 VerifyEmail 邮件通知,正常情况下他默认存储在了 vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php 下。

此 时在 toMail 方法下,来自定义邮件中的用语:

1
2
3
4
5
return (new MailMessage)
->greeting('Hello!')
->line('One of your invoices has been paid!')
->action('View Invoice', $url)
->line('Thank you for using our application!');

sendEmailVerificationNotification

当你通过使用 artisan 命令创建一个邮件通知后,并不代表已经完成了,还需要在 app/User.php 中添加一个 sendEmailVerificationNotification 即“发送电子邮件验证通知”方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

namespace App;

use App\Notifications\VerifyEmail;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
……
public function sendEmailVerificationNotification()
{
$this->notify(new VerifyEmail);
}
}

notifications

既然当 Notifications 以及 sendEmailVerificationNotification 都配置完成了,我们还需要来自定义邮件的模板:

1
php artisan vendor:publish --tag=laravel-notifications

此时会将 /vendor/laravel/framework/src/Illuminate/Notifications/resources/views 内容复制到 /resources/views/vendor/notifications 下。

当然你如果需要将所有 Markdown 通知组件导出自己的项目中进行自定义:

1
php artisan vendor:publish --tag=laravel-mail

Laravel 会将/vendor/laravel/framework/src/Illuminate/Mail/resources/views目录内容复制到/resources/views/vendor/mail下。

需要注意的是,该目录会包含除组件之外的 resources/views/vendor/mail/html/themes目录,该目录下存储着默认的主题文件,您可以自行定义并分享。

重置密码

ResetPassword

在进行下一步之后,我们需要来对重置密码的文本模板进行更改,和 邮箱验证 相差无几,需要通过使用 artisan 命令:

1
php artisan make:notification ResetPassword

之后将从 vendor/laravel/framework/src/Illuminate/Auth/Notifications/ResetPassword.php 文件复制到 app/Notifications/ResetPassword.php中,因此我们可以参考 vendor 内的文件结构写入app/Notifications目录下。

sendPasswordResetNotification

ResetPassword 准备完成之后,我们需要通过在app/User.php文件中添加一个sendPasswordResetNotification方法,来使得 ResetPassword 通知被正常使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

namespace App;

use App\Notifications\VerifyEmail;
use App\Notifications\ResetPassword;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
……
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}
}

⬅️ Go back